<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;

class CategoryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // select c1.*,concat(c1.path,',',c1.id) as path_str,c2.name as pname from category as c1 left JOIN category as c2 on c1.pid = c2.id ORDER BY path_str;
        
        $data = DB::table('category as c1')
            -> leftJoin('category as c2', 'c1.pid', '=', 'c2.id')
            -> select('c1.*',DB::raw("concat(c1.path,',',c1.id) as path_str"), 'c2.name as pname')
            -> orderBy('path_str')
            -> get();
        //
        // $data = DB::table('category') -> select('*', DB::raw("concat(path,',',id) as path_str")) -> orderBy('path_str') -> get();
        foreach($data as $k => $v)
        {
            $num = substr_count($v -> path, ',');
            $str = str_repeat('|----', $num);
            $data[$k] -> name = $str.$v -> name;
        }

        return view('admin.category.index', ['data' => $data, 'title' => '分类列表']);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        $data = DB::table('category') -> select('*', DB::raw("concat(path,',',id) as path_str")) -> orderBy('path_str') -> get();
        foreach($data as $k => $v)
        {
            $num = substr_count($v -> path, ',');
            $str = str_repeat('|----', $num);
            $data[$k] -> name = $str.$v -> name;
        }

        // dd($data);

        return view('admin.category.add', ['title' => '分类添加', 'data' => $data]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $data = $request -> only('name', 'pid');
        // dd($request -> all());
        if($data['pid'] == 0)
        {
            $data['path'] = 0;
        }else
        {
            $parnet_path = DB::table('category') -> where('id', $request -> pid) -> first() -> path;
            $data['path'] = $parnet_path.','.$request -> pid; 
        }

        //添加
        $res = DB::table('category') -> insert($data);
        if($res)
        {
            return redirect('/admin/category') -> with(['info' => '添加成功']);
        }else
        {
            return back() -> with(['info' => '添加失败']);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $data = DB::table('category') -> where('id', $id) -> first();
        
        $allData = DB::table('category') -> select('*', DB::raw("concat(path,',',id) as path_str")) -> orderBy('path_str') -> get();
        foreach($allData as $k => $v)
        {
            $num = substr_count($v -> path, ',');
            $str = str_repeat('|----', $num);
            $allData[$k] -> name = $str.$v -> name;
        }

        return view('admin.category.edit', ['title' => '分类编辑', 'data' => $data, 'allData' => $allData]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $data = $request -> except('_token', '_method');
        if($data['pid'] == 0)
        {
            $data['path'] = 0;
        }else
        {
            $parnet_path = DB::table('category') -> where('id', $request -> pid) -> first() -> path;
            $data['path'] = $parnet_path.','.$request -> pid; 
        }

        $res = DB::table('category') -> where('id', $id) -> update($data);
        if($res)
        {
            return redirect('/admin/category') -> with(['info' => '修改成功']);
        }else
        {
            return back() -> with(['info' => '修改失败']);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $res = DB::table('category') -> where('pid', $id) -> get();
        
        if(!$res)
        {
            $r = DB::table('category') -> delete($id);
            if($r)
            {
                return redirect('/admin/category') -> with(['info' => '删除成功']);
            }else
            {
                return back() -> with(['info' => '删除失败']);
            }
        }else
        {
            return back() -> with(['info' => '对不起，不能删除，有子分类']);
        }
    }
}
